<template>
  <el-button :type="buttonType ? 'text' : ''"
             :icon="buttonType ? '' : icon"
             @click="handleExport">导出</el-button>
</template>

<script>
import { table } from "@/api";
import { mapActions } from "vuex";
import _ from "lodash";
import { mapGetters, mapState } from "vuex";

export default {
  name: "ConsigneeExport",
  props: {
    icon: {
      type: String,
      default: "el-icon-s-promotion"
    },
    args: {
      type: Object,
      default: () => { }
    },
    data: {
      type: Object,
      default: () => { }
    },
    url: String,
    type: [Number, String],
    name: String
  },
  data () {
    return {
      loading: false,
      visible: false,
      userInfo: sessionStorage.getItem('userInfo')
    }
  },
  computed: {
    buttonType () {
      return this.data?.buttonType || ''
    },
    ...mapState({
      menu: (state) => state.menu.menu
    }),
    ...mapGetters('table', ['getMenuFirstRecordSearch']),
    ...mapGetters('table', ['getMenuAdvanceRecordSearch']),
    user () {
      return JSON.parse(this.userInfo)
    },
    userId () {
      return this.user?.userId
    },
    menuId () {
      return this.menu.menuId
    },
    firstNewRecordSearch () {
      return this.getMenuFirstRecordSearch(this.userId, this.menuId)
    },
    advanceNewRecordSearch () {
      return this.getMenuAdvanceRecordSearch(this.userId, this.menuId)
    }
  },
  methods: {
    handleExport: _.debounce(function () {
      // const { metaId, resourceId } = datas
      // if (!this.$table.tableRecords.length) {
      //   this.$message.warning("没有可要导出的数据！");
      //   return false;
      // }
      const filters = { ...this.firstNewRecordSearch, ...this.advanceNewRecordSearch, ...this.args };

      const params = {
        args: { ...filters, ...this.data?.args },
        name: this.name || this.data?.name,
        type: this.data?.type || this.type
      };
      this.exportConfigApi(this.url, params);
    }, 500),
    // 导出配置公共接口
    async exportConfigApi (url, params) {
      let { code } = await table.submitFormContainer(url, params)
      if (code === 0) {
        this.searchCount() //刷新导出未读数
        this.$nextTick(() => {
          this.$message.success("导出功能正在后台执行中，请稍后去“导出文件”下载中查看！");
        });
      }
    },
    ...mapActions({
      searchCount: "login/exportCount"
    }),
  }
}
</script>

<style lang="scss" scoped>
</style>